package com.sky.controller.admin;

import com.sky.domain.User;
import com.sky.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public String UserInfo(Model model){
        User user = userService.getUserInfo();
        model.addAttribute("user",user);
        return "admin/user";
    }

    @GetMapping("/user/{id}/input")
    public String editInput(@PathVariable("id") Long id,Model model){
        User user = userService.getUser(id);
        model.addAttribute("user",user);
        return "admin/user_input";
    }


    @PostMapping("/user")
    public String editPost(User user, HttpSession session, RedirectAttributes attributes){
        userService.updateUserInfo(user);
        session.removeAttribute("user");
        attributes.addFlashAttribute("msg","修改成功");
        return "redirect:/admin/user";
    }

}
